package com.yujianweb;

import java.util.Arrays;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = { 30,20,50,65,5,12,36,54,265,32,152,16};
        int searchWord = 64;
        Arrays.sort(arr);   //二分法查找之前，一定要对数组元素排序

        System.out.println(Arrays.toString(arr));
        System.out.println(searchWord+"元素的索引："+binarySearch(arr,searchWord));
    }

    public static int binarySearch(int[] array, int value){
        int low =0;
        int high = array.length - 1;
        while(low <= high){
            int mid = (low + high) / 2;
            if(value == array[mid]){
                return mid;
            }
            if(value > array[mid]){
                low = mid + 1;
            }
            if(value < array[mid]){
                high = mid - 1;
            }
        }
        return -1;
    }
}
